/** 
 * 安全区域距离底部的距离 
 * @returns {number}
 */
 exports.getSafeToBottom = () => {
  const { screenHeight, safeArea: { bottom } } = wx.getWindowInfo();
  return screenHeight - bottom
}

/**
* 预览图片
* @param {string[]} urls 图片列表
* @param {string} current 当前图片的url
* @returns {void}
*/
exports.prevImg = (urls, current) => wx.previewImage({ urls, current })

/**
* 地图上选择位置
* @param {{lat: number, lng: number}} location 经纬度
* @returns {Promise<{lat: number, lng: number, address: string, name: string}>} 地址信息
*/
exports.chooseLocation = async (location = undefined) => {
  const res = await wx.chooseLocation({ latitude: location?.lat, longitude: location?.lng });
  return { lat: res.latitude, lng: res.longitude, address: res.address, name: res.name }
}

/** 
* 菜单按钮的布局信息
* @returns {{width: number, height: number, top: number, right: number, bottom: number, left: number}}
*/
exports.getMenuButton = () => wx.getMenuButtonBoundingClientRect()

/**
* 获取屏幕安全区域
* @returns {{left: number, right: number, top: number, bottom: number, width: number, height: number}}
*/
exports.getSafeArea = () => wx.getWindowInfo().safeArea

/**
* 获取屏幕状态栏高度
* @returns {number}
*/
exports.getStatusHeight = () => wx.getWindowInfo().statusBarHeight

/**
* 设备定位是否开启
* 微信定位权限是否开启
* @returns {{auth: boolean, enabled: boolean}}
*/
exports.canLocation = () => {
  const auth = wx.getAppAuthorizeSetting().locationAuthorized
  const enabled = wx.getSystemSetting().locationEnabled
  return { auth, enabled }
}

/**
* 判断是否有网
* @returns {Promise<{network: boolean, type: string}>}
*/
exports.hasNetwork = async () => {
  const { networkType } = await wx.getNetworkType()
  return {
      network: networkType !== 'none', // 有无网络
      type: networkType // 网络类型
  }
}

/**
* 打开地图查看位置
* @param {number} lat 纬度
* @param {number} lng 经度
* @param {string} name 位置名
* @param {string} address 地址的详细说明
* @returns {void}
*/
exports.openLocation = (lat, lng, name = undefined, address = undefined) => wx.openLocation({ latitude: lat, longitude: lng, name, address })

/**
* 持续定位获取经纬度
* @returns {Promise<{lat: number, lng: number}>}
*/
exports.getLatLng = () => new Promise((resolve, reject) => {
  const locationChangeFn = res => {
      resolve({ lat: res.latitude, lng: res.longitude });
      wx.offLocationChange(locationChangeFn)
      wx.stopLocationUpdate()
  }
  wx.startLocationUpdate();
  wx.onLocationChange(locationChangeFn)
})

/**
* 获取经纬度
* @param {{lat: number, lng: number}} dl 获取失败时的默认坐标
* @returns {Promise<{lat: number, lng: number}>}
*/
exports.getLatLngOnce = async dl => {
  const res = await wx.getLocation({ type: 'gcj02' }).catch(err => ({ latitude: dl?.lat, longitude: dl?.lng }))
  return { lat: res.latitude, lng: res.longitude }
}

/**
* 获取日期时间
* @param  {...e} e new Date()的参数
* @returns {{
*  Y: number, M: number, D: number, h: number, m: number, s: number,
*  format: (e: string) => string
* }}
*/
exports.getDateTime = (...e) => {
  const d = new Date(...e);
  const f = e => Number(e) < 10 ? `0${e}` : e;
  const o = {
      Y: d.getFullYear(),
      M: f(d.getMonth() + 1),
      D: f(d.getDate()),
      h: f(d.getHours()),
      m: f(d.getMinutes()),
      s: f(d.getSeconds())
  }
  return {
      ...o,
      format: (e = 'Y-M-D h:m:s') => e.split('').map(v => Object.keys(o).includes(v) ? o[v] : v).join('')
  }
}

/**
* 微信登录 wx.login()
* @return {Promise<string>} code
*/
exports.login = () => new Promise((resolve, reject) => wx.login({ success: res => resolve(res.code), fail: err => reject(err) }))

/**
* 无图标的提示框 wx.showToast()
* @param {string} title
* @return {void}
*/
exports.toast = title => wx.showToast({ title, icon: 'none' })

/**
* @callback pageInstance
* @param {object} prevPage 页面实例this
* @returns {void}
*/
/** 
* 上一页处理
* @param {pageInstance} callBack 处理上一页的回调方法
* @returns {void}
*/
exports.prevPage = callBack => {
  const curPages = getCurrentPages()
  const prevPage = curPages[curPages.length - 2]
  callBack(prevPage)
}